<?php

class AppController extends Controller {
    var $helpers = array('Html', 'Form', 'Javascript');
    
     var $components = array('Auth');
        
    function index() {
        $this->{$this->modelClass}->recursive = 0;
        $this->set(strtolower(Inflector::pluralize($this->modelClass)), $this->paginate());        
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__(sprintf('Invalid %s.', $this->modelClass), true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set(strtolower($this->modelClass), $this->{$this->modelClass}->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->{$this->modelClass}->create();
            if ($this->{$this->modelClass}->save($this->data)) {
                $this->Session->setFlash(__(sprintf('The %s has been saved', $this->modelClass), true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__(sprintf('The %s could not be saved. Please, try again.', $this->modelClass), true));
            }
        }
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__(sprintf('Invalid %s', $this->modelClass), true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if ($this->{$this->modelClass}->save($this->data)) {
                $this->Session->setFlash(__(sprintf('The %s has been saved', $this->modelClass), true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__(sprintf('The %s could not be saved. Please, try again.', $this->modelClass), true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->{$this->modelClass}->read(null, $id);
        }
//         $users = $this->{$this->modelClass}->User->find('list');
//         $this->set(compact('users'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__(sprintf('Invalid id for %s', $this->modelClass), true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->{$this->modelClass}->del($id)) {
            $this->Session->setFlash(__(sprintf('%s deleted', $this->modelClass), true));
            $this->redirect(array('action'=>'index'));
        }
    }
    
    function beforeFilter() {
        //Configure AuthComponent
//         $this->Auth->authorize = 'actions';
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'articles', 'action' => 'index');
    }

}

?>